package com.android.bookgarden.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.bookgarden.MyApplication;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.ProvinceidBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.LocationClientUtils;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Create by zouqh
 * on 2018/11/30
 **/
public class InputCityActivity extends BaseActivity implements View.OnClickListener,LocationClientUtils.LocationOcalback,RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.cityInput)
    EditText cityInput;
    RealtConstract.Presenter presenter;
    public static InputCityActivity inputCityActivity;
    private ProvinceidBean bean;
    private String  proviceId;
    private String address;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_city;
    }

    @Override
    protected void initView() {
        inputCityActivity =this;
        presenter = new BasePersenter(this,this);
        titleText.setText("所在城市");
        ritghText.setText("保存");
        ritghText.setVisibility(View.VISIBLE);
        ritghText.setTextColor(Color.parseColor("#23b16e"));
        ritghText.setOnClickListener(this);
        close.setOnClickListener(this);
        cityInput.setOnClickListener(this);
        LocationClientUtils.getLocationIns().initLocation(this,this);
        LocationClientUtils.getLocationIns().startLocation();
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    public void setCity(ProvinceidBean b,String id,String address){
        bean = b;
        cityInput.setText(b.getName());
        proviceId =id;
        this.address = address;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                String str = cityInput.getText().toString();
               if (TextUtils.isEmpty(str)){
                   showToast("请输入修改的城市");
                   return;
               }
                LoadingDialog.showMassge(this,"修改中...");
              Map<String,String> map = new HashMap<>();
                    map.put("provinceid",proviceId);
                map.put("cityid",bean.getId());
                map.put("fulladdr",address);

                    presenter.loadDataGetHeader(map,"", Apis.CHANGE_CITY,1001, Utlis.getHeadler(this));
                break;
            case R.id.cityInput:
                startActivity(SelectCityActivity.class);
                break;
        }
    }




    @Override
    public void getLoactionInfo(String city, double longitude, double Latitude) {
        LocationClientUtils.getLocationIns().stopLocation();
        cityInput.setText(city);

    }

    @Override
    public void loactionFail() {

    }

    @Override
    public void returnData(String data, int urlCode) {

        if (TextUtils.isEmpty(data))
        {
            return;
        }
        handler.obtainMessage(urlCode,data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        LoadingDialog.hideProgress();
        showToast("请求失败");
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String str = (String) msg.obj;
            switch (msg.what){
                case 1001:
                    LoadingDialog.hideProgress();
                    if (TextUtils.isEmpty(str)){
                        return;
                    }

                    DataBean bean = Utlis.getDataBean(str);
                    showToast(bean.getMessage());
                    if (bean.getCode()==200){
                        MyApplication.getInstance().userInfo.setCityid(InputCityActivity.this.bean.getId());
                        MyApplication.getInstance().userInfo.setProvinceid(proviceId);
                        finish();

                    }else{

                    }
                    break;



            }

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        inputCityActivity = null;
    }
}
